home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / plugin / ship / turret.cpp < prev   
Encoding:
C/C++ Source or Header  |  2000-04-10  |  3.9 KB  |  200 lines

  1. #include "game.h"
  2.  
  3. void turret::init()
  4. {
  5.     if (base)
  6.         pos=base->pivotpos;
  7.     tubels=*((local_system *)this);
  8.     tubels.rotate(vector(180,0,0));
  9. }
  10.  
  11. void turret::draw()
  12. {
  13.     active=2;
  14.     if (base==0 || tube==0)
  15.         return;
  16.     if (node)
  17.         {
  18.         base->color=node->color+dynlight;
  19.         tube->color=base->color;
  20.         dynlight.null();
  21.         }
  22.     glPushMatrix();
  23.     glTranslatef(pos.x,pos.y,pos.z);
  24.     glPushMatrix();
  25.     glMultMatrixf((float *)&mat);
  26.     base->draw();
  27.     glPopMatrix();
  28.     glMultMatrixf((float *)&tubels.mat);
  29.     tube->draw();
  30.     glPopMatrix();
  31. }
  32.  
  33. void turret_find(void *data,bsp_object *e)
  34. {
  35.     turret *t=(turret *)data;
  36.     if (e->type==TYPE_SHIP)
  37.         {
  38.         float dist=(e->pos-t->pos).length();
  39.         if (dist<t->enemydist)
  40.             {
  41.             flyengine->excludecollision=e;
  42.             if (flyengine->collision_test(flyengine->bsp,t->pos,e->pos))
  43.                 {
  44.                 flyengine->excludecollision=0;
  45.                 return;
  46.                 }
  47.             flyengine->excludecollision=0;
  48.             t->enemy=e;
  49.             t->enemydist=dist;
  50.             }
  51.         }
  52. }
  53.  
  54. int turret::step(int dt)
  55. {
  56.     // not activated yet by a render
  57.     if (active!=2) return 0;
  58.  
  59.     // if no shield, exlode and die
  60.     if (shield<0.0f)
  61.         {
  62.         life=-1; // die
  63.         if (exp)
  64.             exp->do_explode(pos,Z,-1); // explode
  65.         return 0;
  66.         }
  67.  
  68.     // look around for enemy
  69.     enemy=0;
  70.     enemydist=lookrange;
  71.     flyengine->apply_bsp(flyengine->bsp,pos,lookrange,this,turret_find);
  72.  
  73.     // if enemy found
  74.     if (enemy)
  75.     {
  76.         // get enemy direction and try to align cannon
  77.         vector dir=pos-enemy->pos;
  78.         dir.normalize();
  79.         tubels.rotate(tubels.Z,dir,rotvel*dt);
  80.  
  81.         // if cannon is inside fire angle threshold
  82.         if (g && vec_dot(dir,tubels.Z)>fireang)
  83.             if (g->fire_status())
  84.             {
  85.             local_system ls=*((local_system *)this);
  86.             *((local_system *)this)=tubels;
  87.             g->fire(this,-1);
  88.             *((local_system *)this)=ls;
  89.             }
  90.     }
  91.  
  92.     return 0;
  93. }
  94.  
  95. int turret::get_custom_param_desc(int i,param_desc *pd)
  96. {
  97.     if (pd==0)
  98.         return 8;
  99.     else 
  100.     switch(i)
  101.     {
  102.         case 0:
  103.             pd->type='3';
  104.             pd->data=&base;
  105.             strcpy(pd->name,"base");
  106.             break;
  107.         case 1:
  108.             pd->type='3';
  109.             pd->data=&tube;
  110.             strcpy(pd->name,"tube");
  111.             break;
  112.         case 2:
  113.             pd->type=TYPE_GUN;
  114.             pd->data=&g;
  115.             strcpy(pd->name,"gun");
  116.             break;
  117.         case 3:
  118.             pd->type='f';
  119.             pd->data=&lookrange;
  120.             strcpy(pd->name,"lookrange");
  121.             break;
  122.         case 4:
  123.             pd->type='a';
  124.             pd->data=&fireang;
  125.             strcpy(pd->name,"fireang");
  126.             break;
  127.         case 5:
  128.             pd->type='f';
  129.             pd->data=&shield;
  130.             strcpy(pd->name,"shield");
  131.             break;
  132.         case 6:
  133.             pd->type=TYPE_EXPLODE;
  134.             pd->data=&exp;
  135.             strcpy(pd->name,"explode");
  136.             break;
  137.         case 7:
  138.             pd->type='f';
  139.             pd->data=&rotvel;
  140.             strcpy(pd->name,"rotvel");
  141.             break;
  142.     }
  143.     return 0;
  144. }
  145.  
  146. int turret::message(vector& p,float rad,int msg,int param,void *data)
  147. {
  148.     if (msg==FLYOBJM_DAMAGE)
  149.     {
  150.         vector v=pos-p;
  151.         float len=v.length();
  152.         if (len>rad || len<SMALL)
  153.             return 0;
  154.  
  155.         flyengine->excludecollision=this;
  156.         if(flyengine->collision_test(flyengine->bsp, p, pos))
  157.         {
  158.             flyengine->excludecollision=0;
  159.             return 0;
  160.         }
  161.         flyengine->excludecollision=0;
  162.  
  163.         shield-=*((float *)data)*(1.0f-len/rad);
  164.     }
  165.     else
  166.     if (msg==FLYOBJM_ILLUM)
  167.     {
  168.         float fac=(p-pos).length()/rad;
  169.         if (fac<1.0f)
  170.             dynlight+=*((vector *)data)*(1.0f-fac);
  171.     }
  172.  
  173.     return 0;
  174. }
  175.  
  176. mesh *turret::ray_intersect(vector& ro,vector& rd,vector& ip,float& dist,int &facenum,float rad)
  177. {
  178.     if (base)
  179.     {
  180.         static float d1,d2;
  181.         vector 
  182.             ro_local=(ro-pos)*mat_t,
  183.             rd_local=rd*mat_t;
  184.         if (base->bbox.ray_intersect(ro_local,rd_local,d1,d2))
  185.         {
  186.         facenum=base->ray_intersect(ro_local,rd_local,ip,dist,rad);
  187.         if (facenum>-1)
  188.             {
  189.             if (flyengine->stepobj && 
  190.                 (flyengine->stepobj->type==TYPE_LASER || flyengine->stepobj->type==TYPE_MISSILE) 
  191.                 && dist<1.0f && ((gun_projectile *)flyengine->stepobj)->damage>0.0f)
  192.                 shield-=((gun_projectile *)flyengine->stepobj)->damage;
  193.             ip=ip*mat+pos;
  194.             return base;
  195.             }
  196.         }
  197.     }
  198.     return 0;
  199. }
  200.